#!/bin/sh

#To fix bug 1-12UQ1H
rm -fdR /Library/Receipts/Job\ Properties.pkg
sudo rm -fdR /Library/Receipts/Job\ Properties.pkg

installer_mpkg_path=$1
ins_path=`echo $installer_mpkg_path|sed 's/OSX installer.mpkg//g'`
#echo "Path : $ins_path "

 chown $USER:staff "$1/Contents/Resources/fierycupsfilter"
 chmod 775 "$1/Contents/Resources/fierycupsfilter"

 sudo chown $USER:staff "$1/Contents/Resources/fierycupsfilter"
 sudo chmod 775 "$1/Contents/Resources/fierycupsfilter"


####Create ppd plugins and ppds directory structure
mkdir "/Library/Printers/PPD Plugins"
chmod -R 755 "/Library/Printers/PPD Plugins"

mkdir "/Library/Printers/PPDs"
chmod -R 755 "/Library/Printers/PPDs"

mkdir "/Library/Printers/PPDs/Contents"
chmod -R 755 "/Library/Printers/PPDs/Contents"

mkdir "/Library/Printers/PPDs/Contents/Resources"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources"

#Make English PPD sub-folders first. PPDs are copied later.
mkdir "/Library/Printers/PPDs/Contents/Resources/en.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/en.lproj"

mkdir "/Library/Printers/PPDs/Contents/Resources/cs.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/cs.lproj"
if [ -d "$ins_path/Czech" ]
then
cp -Rp "$ins_path/Czech/" /Library/Printers/PPDs/Contents/Resources/cs.lproj

cp -Rpn "$ins_path/Czech/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Czech"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/da.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/da.lproj"
if [ -d "$ins_path/Dansk" ]
then
cp -Rp "$ins_path/Dansk/" /Library/Printers/PPDs/Contents/Resources/da.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/da.lproj

cp -Rpn "$ins_path/Dansk/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Dansk"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/de.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/de.lproj"
if [ -d "$ins_path/Deutsch" ]
then
cp -Rp "$ins_path/Deutsch/" /Library/Printers/PPDs/Contents/Resources/de.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/de.lproj"

cp -Rpn "$ins_path/Deutsch/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Deutsch"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/es.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/es.lproj"
if [ -d "$ins_path/Espanol/" ]
then
cp -Rp "$ins_path/Espanol/" /Library/Printers/PPDs/Contents/Resources/es.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/es.lproj"

cp -Rpn "$ins_path/Espanol/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Espanol"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/fi.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/fi.lproj"
if [ -d "$ins_path/Suomi" ]
then
cp -Rp "$ins_path/Suomi/" /Library/Printers/PPDs/Contents/Resources/fi.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/fi.lproj"

cp -Rpn "$ins_path/Suomi/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Suomi"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/fr.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/fr.lproj"
if [ -d "$ins_path/Francais/" ]
then
cp -Rp "$ins_path/Francais/" /Library/Printers/PPDs/Contents/Resources/fr.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/fr.lproj"

cp -Rpn "$ins_path/Francais/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Francais"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/it.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/it.lproj"
if [ -d "$ins_path/Italiano" ]
then
cp -Rp "$ins_path/Italiano/" /Library/Printers/PPDs/Contents/Resources/it.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/it.lproj"

cp -Rpn "$ins_path/Italiano/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Italiano"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/ru.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/ru.lproj"
if [ -d "$ins_path/Russian" ]
then
cp -Rp "$ins_path/Russian/" /Library/Printers/PPDs/Contents/Resources/ru.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/ru.lproj"

cp -Rpn "$ins_path/Russian/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Russian"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/ja.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/ja.lproj"
if [ -d "$ins_path/Japanese" ]
then
cp -Rp "$ins_path/Japanese/" /Library/Printers/PPDs/Contents/Resources/ja.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/ja.lproj"

cp -Rpn "$ins_path/Japanese/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Japanese"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/ko.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/ko.lproj"
if [ -d "$ins_path/Korean" ]
then
cp -Rp "$ins_path/Korean/" /Library/Printers/PPDs/Contents/Resources/ko.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/ko.lproj"

cp -Rpn "$ins_path/Korean/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Korean"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/nl.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/nl.lproj"
if [ -d "$ins_path/Nederlands" ]
then
cp -Rp "$ins_path/Nederlands/" /Library/Printers/PPDs/Contents/Resources/nl.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/nl.lproj"

cp -Rpn "$ins_path/Nederlands/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Nederlands"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/no.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/no.lproj"
if [ -d "$ins_path/Norsk" ]
then
cp -Rp "$ins_path/Norsk/" /Library/Printers/PPDs/Contents/Resources/no.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/no.lproj"

cp -Rpn "$ins_path/Norsk/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Norsk"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/pt_BR.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/pt_BR.lproj"
if [ -d "$ins_path/Portuguese/" ]
then
cp -Rp "$ins_path/Portuguese/" /Library/Printers/PPDs/Contents/Resources/pt_BR.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/pt_BR.lproj"

cp -Rpn "$ins_path/Portuguese/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Portuguese"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/sv.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/sv.lproj"
if [ -d "$ins_path/Svenska" ]
then
cp -Rp "$ins_path/Svenska/" /Library/Printers/PPDs/Contents/Resources/sv.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/sv.lproj"

cp -Rpn "$ins_path/Svenska/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Svenska"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/zh_TW.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/zh_TW.lproj"
if [ -d "$ins_path/Traditional_Chinese" ]
then
cp -Rp "$ins_path/Traditional_Chinese/" /Library/Printers/PPDs/Contents/Resources/zh_TW.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/zh_TW.lproj"

cp -Rpn "$ins_path/Traditional_Chinese/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Traditional_Chinese"
fi

mkdir "/Library/Printers/PPDs/Contents/Resources/zh_CN.lproj"
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/zh_CN.lproj"
if [ -d "$ins_path/Simplified_Chinese" ]
then
cp -Rp "$ins_path/Simplified_Chinese/" /Library/Printers/PPDs/Contents/Resources/zh_CN.lproj
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/zh_CN.lproj"

cp -Rpn "$ins_path/Simplified_Chinese/" /Library/Printers/PPDs/Contents/Resources/en.lproj
chmod -R 755 /Library/Printers/PPDs/Contents/Resources/en.lproj

folderLang="Simplified_Chinese"
fi


if [ -d "$ins_path/English" ]
then
Echo "############INside English#####"
cp -Rp "$ins_path/English/" /Library/Printers/PPDs/Contents/Resources/en.lproj/
chmod -R 755 "/Library/Printers/PPDs/Contents/Resources/en.lproj"
folderLang="English"
fi

#getting the os version
installCocoa=0
os_vers=`sw_vers -productVersion | cut -c 1-4`
echo "OS Version: $os_vers"
os_vers=`echo $os_vers | tr -d '\.'`
if [ $os_vers -ge 105 ]
then
        echo "OS Version 10.5 or higher"
        installCocoa=1
elif [ $os_vers -ge 103 ]
then
        echo "10.3 or higher"
fi

echo "FolderLanguage: $folderLang"

if [ -d "$ins_path"English ]
then
	filetoChose=`ls "$ins_path"English/ | tail -1`
	echo "Name of the PPD lang: English"
	oem_var=`tr '\r' '\n' <"$ins_path"English/"$filetoChose" | awk -F"/" '/cupsFilter/{print $5}'`
	#get pcfilename
	PCFILENAME=`tr '\r' '\n' <"$ins_path"English/"$filetoChose" | grep "APDialogExtension"|awk -F"\"" '{print $2}' | awk -F"/EF" '{print $2}' | awk -F" " '{print $1}'`
	#get plugins path
	PLUGINPATH=`tr '\r' '\n' <"$ins_path"English/"$filetoChose" | grep "APDialogExtension"|awk -F"\"" '{print $2}' | awk -F"/EF" '{print $1}'`
	#get cupsfilter path
	FILTERPATH=`tr '\r' '\n' <"$ins_path"English/"$filetoChose" | grep -i "fierycupsFilter" | awk -F"\"" '{print $2}' | awk -F"/Library" '{print $2}' | awk -F"fierycupsfilter" '{print $1}'`
else
	filetoChose=`ls "$ins_path$folderLang/" | tail -1`
	echo "Name of the PPD lang: $folderLang"
	oem_var=`tr '\r' '\n' <"$ins_path$folderLang/$filetoChose" | awk -F"/" '/cupsFilter/{print $5}'`
	PCFILENAME=`tr '\r' '\n' <"$ins_path$folderLang/$filetoChose" | grep "APDialogExtension"|awk -F"\"" '{print $2}' | awk -F"/EF" '{print $2}' | awk -F" " '{print $1}'`
	#get plugins path
	PLUGINPATH=`tr '\r' '\n' <"$ins_path"$folderLang/"$filetoChose" | grep "APDialogExtension"|awk -F"\"" '{print $2}' | awk -F"/EF" '{print $1}'`
	#get cupsfilter path
	FILTERPATH=`tr '\r' '\n' <"$ins_path"$folderLang/"$filetoChose" | grep -i "fierycupsFilter" | awk -F"\"" '{print $2}' | awk -F"/Library" '{print $2}' | awk -F"fierycupsfilter" '{print $1}'`
fi

if [ $PCFILENAME == "" ]
then
	echo "PCFILENAME is null !!"
	exit 65
else
	PCFILENAME="EF$PCFILENAME"
fi

if [ $FILTERPATH == "" ]
then
	echo "FieryCupsfilter path is null !!"
	exit 65
else
	FILTERPATH="/Library$FILTERPATH"
fi

if [ $PLUGINPATH == "" ]
then
	echo "Fiery Featuresplugins path is null !!"
	exit 65
fi

echo "PCFILENAME = $PCFILENAME"
echo "CupsFilterPath = $FILTERPATH"
echo "Plugins Path = $PLUGINPATH"
echo "$PCFILENAME" >/Applications/.djp2.log

if [ ! -d /Library/Application\ Support/EFI/driver/$PCFILENAME ]
then
	mkdir /Library/Application\ Support/EFI/driver/$PCFILENAME
fi

echo "Name of the PPD: $filetoChose"
oem_var=`tr '\r' '\n' <"$ins_path$folderLang/$filetoChose" | awk -F"/" '/cupsFilter/{print $5}'`
#echo $oem_var > oemvar
echo "oem_var: $oem_var"

#flag for checking whether the OEM part's are executed
flag=0

#cocoa plugin folder name
cocoaFolder="PluginX64"

if [ "$oem_var" = "Canon" ]
then
	flag=1
	echo "Inside Canon"
	#get the plugin path
 	mkdir -p "$PLUGINPATH" 
 	chmod -R 775 "$PLUGINPATH"
 	mkdir -p "$FILTERPATH" 
 	chmod -R 775 "$FILTERPATH"
 	ditto -rsrc "$1/Contents/Resources/fierycupsfilter" "$FILTERPATH"
 	sudo chown root:admin $FILTERPATH"fierycupsfilter"
 	sudo chmod 775 $FILTERPATH"fierycupsfilter"
	if [ $installCocoa == 1 ]
        then
                echo "Copying plugins from PluginX64 folder"
                echo "Plugin path##### $ins_path$cocoaFolder/*.plugin"
                cp -Rp "$ins_path$cocoaFolder"/*.plugin "$PLUGINPATH/"
        else
                echo "Copying plugins from Printer Driver folder"
                echo "Plugin Path: $ins_path*.plugin"
                cp -Rp "$ins_path"*.plugin "$PLUGINPATH/"
        fi

fi
#echo $SHELL > testshell

echo "After Cannon"

if [ "$oem_var" = "EFI" -o "$oem_var" = "efi" -o "$oem_var" = "Fiery" -o "$oem_var" = "fiery" ]
then
	flag=1
 	oem_folder="/Library/Printers/efi"
	mkdir "$oem_folder"
 	chmod 775 "$oem_folder"
	mkdir "$oem_folder/Filters"
 	chmod 775 "$oem_folder/Filters"
 	mkdir "$oem_folder/PDEs"
 	chmod 775 "$oem_folder/PDEs"
 	ditto -rsrc "$1/Contents/Resources/fierycupsfilter" "/Library/Printers/efi/Filters/"
 	sudo chown root:admin "/Library/Printers/efi/Filters/fierycupsfilter"
 	sudo chmod 775 "/Library/Printers/efi/Filters/fierycupsfilter"
	if [ $installCocoa == 1 ]
        then
                echo "Copying plugins from PluginX64 folder"
                echo "Plugin path##### $ins_path$cocoaFolder/*.plugin"
                cp -Rp "$ins_path$cocoaFolder"/*.plugin "/Library/Printers/$oem_var/PDEs/"
        else
                echo "Copying plugins from Printer Driver folder"
                echo "Plugin Path: $ins_path*.plugin"
                cp -Rp "$ins_path"*.plugin "/Library/Printers/$oem_var/PDEs/"
        fi
fi


echo "After efi"

if [ "$oem_var" = "Xerox" ]
then
 	flag=1
 	echo "OEM VAR : $oem_var"  
 	oem_folder="/Library/Printers/$oem_var"
 	mkdir "$oem_folder"
 	chmod 775 "$oem_folder"
 	mkdir "$oem_folder/Filters"
 	chmod 775 "$oem_folder/Filters"
 	mkdir "$oem_folder/PDEs"
 	chmod 775 "$oem_folder/PDEs" 
 	echo "Plugin path##### $ins_path*.plugin"
 	ditto -rsrc "$1/Contents/Resources/fierycupsfilter" "/Library/Printers/$oem_var/Filters/"
	sudo chown root:admin "/Library/Printers/$oem_var/Filters/fierycupsfilter"
 	sudo chmod 775 "/Library/Printers/$oem_var/Filters/fierycupsfilter"
        if [ $installCocoa == 1 ]
        then
                echo "Copying plugins from PluginX64 folder"
                echo "Plugin path##### $ins_path$cocoaFolder/*.plugin"
                cp -Rp "$ins_path$cocoaFolder"/*.plugin "/Library/Printers/$oem_var/PDEs/"
        else
                echo "Copying plugins from Printer Driver folder"
                echo "Plugin Path: $ins_path*.plugin"
                cp -Rp "$ins_path"*.plugin "/Library/Printers/$oem_var/PDEs/"
        fi
fi


if [ "$oem_var" = "Fuji Xerox" ]
then
 	flag=1
 	oem_folder="/Library/Printers/FujiXerox"
 	mkdir "$oem_folder"
 	chmod 775 "$oem_folder"
 	mkdir "$oem_folder/Filters"
 	chmod 775 "$oem_folder/Filters"
 	mkdir "$oem_folder/PDEs"
 	chmod 775 "$oem_folder/PDEs"
 	ditto -rsrc "$1/Contents/Resources/fierycupsfilter" "/Library/Printers/$oem_var/Filters/"
 	sudo chown root:admin "/Library/Printers/$oem_var/Filters/fierycupsfilter"
 	sudo chmod 775 "/Library/Printers/$oem_var/Filters/fierycupsfilter"
        if [ $installCocoa == 1 ]
        then
                echo "Copying plugins from PluginX64 folder"
                echo "Plugin path##### $ins_path$cocoaFolder/*.plugin"
                cp -Rp "$ins_path$cocoaFolder"/*.plugin "/Library/Printers/$oem_var/PDEs/"
        else
                echo "Copying plugins from Printer Driver folder"
                echo "Plugin Path: $ins_path*.plugin"
                cp -Rp "$ins_path"*.plugin "/Library/Printers/$oem_var/PDEs/"
        fi
fi


echo "After xerox"

if [ "$oem_var" != "" -a $flag == 0 ]
then
 	oem_folder="/Library/Printers/$oem_var"
 	mkdir "$oem_folder"
 	chmod 775 "$oem_folder"
 	mkdir "$oem_folder/Filters"
 	chmod 775 "$oem_folder/Filters"
 	mkdir "$oem_folder/PDEs"
 	chmod 775 "$oem_folder/PDEs"
 	ditto -rsrc "$1/Contents/Resources/fierycupsfilter" "/Library/Printers/$oem_var/Filters/"
 	sudo chown root:admin "/Library/Printers/$oem_var/Filters/fierycupsfilter"
 	sudo chmod 775 "/Library/Printers/$oem_var/Filters/fierycupsfilter"
        if [ $installCocoa == 1 ]
        then
                echo "Copying plugins from PluginX64 folder"
                echo "Plugin path##### $ins_path$cocoaFolder/*.plugin"
                cp -Rp "$ins_path$cocoaFolder"/*.plugin "/Library/Printers/$oem_var/PDEs/"
        else
                echo "Copying plugins from Printer Driver folder"
                echo "Plugin Path: $ins_path*.plugin"
                cp -Rp "$ins_path"*.plugin "/Library/Printers/$oem_var/PDEs/"
        fi
elif [ "$oem_var" == "" ]
then
 	ditto -rsrc "$1/Contents/Resources/fierycupsfilter" "/usr/libexec/cups/filter/"
 	sudo chown root:admin "/usr/libexec/cups/filter/fierycupsfilter"
 	sudo chmod 775 "/usr/libexec/cups/filter/fierycupsfilter"
	echo "Install here" > "/usr/libexec/cups/filter/installbait"
fi

echo "After all"
exit 0
